﻿<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Web.UI.WebControls.WebParts" %>
<%@ Import Namespace="System.Web.UI.HtmlControls" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="ZhiAo.Management" %>
<%@ Import Namespace="ECI.SSO.BizEntity" %>

<%@ Page Language="C#" %>

<script runat="server">
    //Webconfig文件中获取数据库路径
    public string g_StrConn = ConfigurationManager.AppSettings["Conn_Access"];
    protected void Page_Load(object sender, EventArgs e)
    {
        string mode = Request.QueryString["mode"];
        switch (mode)
        { //51/a/spx///www.codefans.net
            case "getTree":
                Response.Write(getTree());
                break;
            case "getAdd":
                Response.Write(getAdd());
                break;
            case "saveAdd":
                Response.Write(saveAdd());
                break;
            case "getEdit":
                Response.Write(getEdit());
                break;
            case "saveEdit":
                Response.Write(saveEdit());
                break;
            case "addSub":
                Response.Write(addSub());
                break;
            case "del":
                Response.Write(del());
                break;
            case "returnEdit":
                Response.Write(returnEdit());
                break;
        }
    }

    //******************
    //**  获取树结构  **
    //******************
    public string getTree()
    {
        string fatherID = Request.QueryString["fatherID"];
        string root = "";

        try
        {
            string TopStr = "<a id='add" + fatherID + "'></a><table border='0' cellpadding='0' cellspacing='0' style='width: 100%; height: 10px'>";
            string BottomStr = "</table>";
            string MiddleStr = "";
            Function[] fun = AjaxMenuInfo.GetLowerFunctionsForUser(fatherID);

            foreach (Function item in fun)
            {
                MiddleStr = MiddleStr + "<tr><td align='left' valign='top' style='width:200px;height:22px;'  colspan='2' class=\"menue\">";
                try
                {
                    if (item.ShowInMenu)
                        MiddleStr = MiddleStr + "<img alt='' id='AddImg" + item.ID + "' src='Images/nolines_plus.gif' onclick=getdata('tree.aspx?mode=getTree&fatherID=" + item.ID + "','sub" + item.ID + "');ShowSub('" + item.ID + "'); /><img alt='' src='Images/afolder.gif' /><a id='edit" + item.ID + "' href=\"" + item.URL + "?functionId=" + item.ID + "\">" + item.Name.Trim() + "</a>";
                    else
                        MiddleStr = MiddleStr + "<img alt='' src='Images/page.gif' /><a id='edit" + item.ID + "' href=\"" + item.URL + "?functionId=" + item.ID + "\">" + item.Name.Trim() + "</a>";
                }
                catch
                {
                    return "<span style='font-size:12px; color:Black;'>数据库连接失败！</span>";
                }
                MiddleStr = MiddleStr + "</span></td></tr><tr><td align='left' valign='middle' style='width:22px;'></td><td align='left' valign='middle' ><div style='display:none;' id='sub" + item.ID + "'></div></td></tr>";
            }
            return root + TopStr + MiddleStr + BottomStr;
        }
        catch
        {
            return "<span style='font-size:12px; color:Black;'>数据库连接失败！</span>";
        }
    }

    //************************
    //**  获取节点新增表单  **
    //************************
    public string getAdd()
    {
        string addHtml = "";
        string fatherID = Request.QueryString["fatherID"];
        if (!IsNumber(fatherID))
        {
            return "<span style='font-size:12px; color:Black;'>禁止提交非法的数据！</span>";
        }
        addHtml = addHtml + "<span style='font-size:11px; color:Black;'>&nbsp;&nbsp;&nbsp;</span><img alt='' src='Images/folder.bmp' />&nbsp;<input onblur=postAdd(" + fatherID + ") name='addName" + fatherID + "' type='text' value='新建菜单项' size='15'><br />";
        return addHtml;
    }

    //**********************
    //**  保存新增的节点  **
    //**********************
    public string saveAdd()
    {
        string id = Request.QueryString["id"];
        string Name = Request["Name"];
        if (Name.CompareTo("") == 0)
        {
            return "nameNull";
        }
        string fatherID = Request["fatherID"];
        if (!IsNumber(fatherID))
        {
            return "<span style='font-size:12px; color:Black;'>禁止提交非法的数据！</span>";
        }
        if (!checkName(Name, ""))
        {
            return "addNameRep" + fatherID;
        }
        string StrSql = "Select * From tree";
        OleDbConnection MyConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath(g_StrConn));
        try
        {
            MyConn.Open();
            OleDbDataAdapter odAdapt = new OleDbDataAdapter(StrSql, MyConn);
            OleDbCommandBuilder oCB = new OleDbCommandBuilder(odAdapt);
            DataSet DS = new DataSet();
            odAdapt.Fill(DS);
            DataRow DR = DS.Tables[0].NewRow();
            DR["name"] = Name;
            DR["fatherID"] = fatherID;
            DS.Tables[0].Rows.Add(DR);
            odAdapt.Update(DS);
            MyConn.Close();
            return "added" + fatherID;
        }
        catch
        {
            return "<span style='font-size:12px; color:Black;'>数据库连接失败！</span>";
        }
    }

    //********************
    //**  新增子级节点  **
    //********************
    public string addSub()
    {
        string changeHtml = "";
        string id = Request.QueryString["id"];
        if (!IsNumber(id))
        {
            return "<span style='font-size:12px; color:Black;'>禁止提交非法的数据！</span>";
        }
        string StrSql = "Select * From tree Where id=" + id;
        OleDbConnection MyConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath(g_StrConn));
        try
        {
            MyConn.Open();
            OleDbDataAdapter odAdapt = new OleDbDataAdapter(StrSql, MyConn);
            DataSet DS = new DataSet();
            odAdapt.Fill(DS);
            DataTable DT = DS.Tables[0];
            changeHtml = changeHtml + "<img alt='' id='AddImg" + DT.Rows[0]["ID"].ToString() + "' src='Images/addImg.bmp' onclick=getdata('tree.aspx?mode=getTree&fatherID=" + DT.Rows[0]["ID"].ToString() + "','sub" + DT.Rows[0]["ID"].ToString() + "');ShowSub(" + DT.Rows[0]["ID"].ToString() + "); />&nbsp;<img alt='' src='Images/folder.bmp' />&nbsp;<a id='edit" + DT.Rows[0]["ID"].ToString() + "'><span style='cursor:hand;font-size:12px; color:Black;' oncontextmenu=showMenu(" + DT.Rows[0]["ID"].ToString() + "," + DT.Rows[0]["fatherID"].ToString() + "); onclick=alert('" + DT.Rows[0]["name"].ToString() + "'); >" + DT.Rows[0]["name"].ToString() + "</span></a>";
            MyConn.Close();
        }
        catch
        {
            return "<span style='font-size:12px; color:Black;'>数据库连接失败！</span>";
        }
        return changeHtml;
    }

    //************************
    //**  获取节点修改表单  **
    //************************
    public string getEdit()
    {
        string editHtml = "";
        string startValue = "";
        string id = Request.QueryString["id"];
        if (!IsNumber(id))
        {
            return "<span style='font-size:12px; color:Black;'>禁止提交非法的数据！</span>";
        }
        string StrSql = "Select * From tree Where id=" + id;
        OleDbConnection MyConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath(g_StrConn));
        try
        {
            MyConn.Open();
            OleDbDataAdapter odAdapt = new OleDbDataAdapter(StrSql, MyConn);
            DataSet DS = new DataSet();
            odAdapt.Fill(DS);
            DataTable DT = DS.Tables[0];
            startValue = DT.Rows[0]["name"].ToString().Trim();
            MyConn.Close();
            editHtml = editHtml + "<input onblur=postEdit(" + id + ") name='editName" + id + "' type='text' value='" + startValue + "' size='15'>";
            return editHtml;
        }
        catch
        {
            return "<span style='font-size:12px; color:Black;'>数据库连接失败！</span>";
        }
    }

    //**********************
    //**  保存修改的节点  **
    //**********************
    public string saveEdit()
    {
        string id = Request.QueryString["id"];
        if (!IsNumber(id))
        {
            return "<span style='font-size:12px; color:Black;'>禁止提交非法的数据！</span>";
        }
        string Name = Request["Name"];
        if (Name.CompareTo("") == 0)
        {
            return "nameNull";
        }
        if (!checkName(Name, id))
        {
            return "editNameRep" + id;
        }
        string StrSql = "Select * From tree Where id=" + id;
        OleDbConnection MyConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath(g_StrConn));
        try
        {
            MyConn.Open();
            OleDbDataAdapter odAdapt = new OleDbDataAdapter(StrSql, MyConn);
            OleDbCommandBuilder oCB = new OleDbCommandBuilder(odAdapt);
            DataSet DS = new DataSet();
            odAdapt.Fill(DS);
            DataTable DT = DS.Tables[0];
            DT.Rows[0]["name"] = Name;
            odAdapt.Update(DS);
            MyConn.Close();
            return "edited" + id;
        }
        catch
        {
            return "<span style='font-size:12px; color:Black;'>数据库连接失败！</span>";
        }
    }

    //************************
    //**  返回修改后的节点  **
    //************************
    public string returnEdit()
    {
        string editHtml = "";
        string startValue = "";
        string fatherID = "";
        string id = Request.QueryString["id"];
        if (!IsNumber(id))
        {
            return "<span style='font-size:12px; color:Black;'>禁止提交非法的数据！</span>";
        }
        string StrSql = "Select * From tree Where id=" + id;
        OleDbConnection MyConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath(g_StrConn));
        try
        {
            MyConn.Open();
            OleDbDataAdapter odAdapt = new OleDbDataAdapter(StrSql, MyConn);
            DataSet DS = new DataSet();
            odAdapt.Fill(DS);
            DataTable DT = DS.Tables[0];
            startValue = DT.Rows[0]["name"].ToString().Trim();
            fatherID = DT.Rows[0]["fatherID"].ToString().Trim();
            MyConn.Close();
            editHtml = editHtml + "<span style='cursor:hand;font-size:12px; color:Black;' oncontextmenu=showMenu(" + id + "," + fatherID + "); onclick=alert('" + startValue + "'); >" + startValue + "</span>";
            return editHtml;
        }
        catch
        {
            return "<span style='font-size:12px; color:Black;'>数据库连接失败！</span>";
        }
    }

    //****************
    //**  删除节点  **
    //****************
    public string del()
    {
        string id = Request.QueryString["id"];
        if (!IsNumber(id))
        {
            return "<span style='font-size:12px; color:Black;'>禁止提交非法的数据！</span>";
        }
        string fatherID = Request.QueryString["fatherID"];
        if (!IsNumber(fatherID))
        {
            return "<span style='font-size:12px; color:Black;'>禁止提交非法的数据！</span>";
        }
        string StrSql = "select * From Tree where fatherID=" + id;
        OleDbConnection MyConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath(g_StrConn));
        OleDbCommand MyCmd = new OleDbCommand(StrSql, MyConn);
        try
        {
            MyCmd.Connection.Open();
            OleDbDataReader Dr = MyCmd.ExecuteReader();
            if (Dr.Read())
            {
                return "deletError" + fatherID;
            }
            MyCmd.Connection.Close();
        }
        catch
        {
            return "<span style='font-size:12px; color:Black;'>数据库连接失败！</span>";
        }
        string StrSql2 = "Delete * From Tree where id=" + id;
        OleDbConnection MyConn2 = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath(g_StrConn));
        OleDbCommand myCmd2 = new OleDbCommand(StrSql2, MyConn2);
        try
        {
            MyConn2.Open();
            myCmd2.ExecuteReader();
            MyConn2.Close();
            return "deleted" + fatherID;
        }
        catch
        {
            return "<span style='font-size:12px; color:Black;'>数据库连接失败！</span>";
        }
    }

    //************************
    //**  判断名字是否存在  **
    //************************
    public bool checkName(string name, string id)
    {
        name = MoveBidStr(name);
        string StrSql = "";
        if (id.CompareTo("") == 0)
        {
            StrSql = "select * from Tree where name='" + name + "'";
        }
        else
        {
            StrSql = "select * from Tree where name='" + name + "' and id<>" + id;
        }
        OleDbConnection MyConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath(g_StrConn));
        OleDbCommand MyCmd = new OleDbCommand(StrSql, MyConn);
        try
        {
            MyCmd.Connection.Open();
            OleDbDataReader Dr = MyCmd.ExecuteReader();
            if (Dr.Read())
            {
                return false;
            }
            else
            {
                return true;
            }
            MyCmd.Connection.Close();
        }
        catch
        {
            return false;
        }
    }
    //**************************
    //**  判断是否是数字组合  **
    //**************************
    public bool IsNumber(string strDate)
    {
        if (strDate == null)
        {
            return false;
        }
        if (strDate.Equals(string.Empty))
        {
            return false;
        }

        Regex numRegex = new Regex(@"0*[0-9][0-9]*$");
        return numRegex.IsMatch(strDate);
    }
    //******************
    //**  过滤'"符号  **
    //******************
    public string MoveBidStr(string strString)
    {
        string str;
        str = strString;
        if (str != null)
        {
            str = str.Replace("'", "''");
            str = str.Replace(((char)39).ToString(), ((char)39 + (char)39).ToString());
        }
        return str;
    }
</script>

